package org.firebug.common.core.api;

import java.io.Serializable;

/**
 * 分页工具
 * @author Felix
 */
public class Pagination implements Serializable {
    private static final int MIN_PN = 1;
    private static final int MIN_PS = 10;
    private static final int MAX_PS = 100;
    /** page number */
    private int pn;
    /** page size */
    private int ps;
    /** record count */
    private int rct;

    public Pagination() {
        this(1, 20, 0);
    }

    public Pagination(int pn, int ps, int rct) {
        this.setPn(pn);
        this.setPs(ps);
        this.setRct(rct);
    }

    public int getPn() {
        return this.pn;
    }

    public void setPn(int pn) {
        this.pn = pn < MIN_PN ? MIN_PN : pn;
    }

    public int getPs() {
        return this.ps;
    }

    public void setPs(int ps) {
        if (ps < MIN_PS) {
            this.ps = MIN_PS;
        } else if (MAX_PS < ps) {
            this.ps = MAX_PS;
        } else {
            this.ps = ps;
        }
    }

    public int getRct() {
        return this.rct;
    }

    public void setRct(int rct) {
        this.rct = 0 < rct ? rct : 0;
    }

    /** page count */
    public int getPct() {
        int pct = this.rct / this.ps;
        if (pct == 0 || this.rct % this.ps != 0) {
            pct++;
        }
        return pct;
    }

    /** mysql offset */
    public int offset() {
        return (pn - 1) * ps;
    }

    /** mysql limit */
    public int limit() {
        return this.ps;
    }

    /** oracle start rowno */
    public int startRowno() {
        return (pn - 1) * ps + 1;
    }

    /** oracle end rowno */
    public int endRowno() {
        return pn * ps;
    }

    /** solr start */
    public int start() {
        return (pn - 1) * ps + 1;
    }

    /** solr rows */
    public int rows() {
        return ps;
    }
}
